package gcs.Interface;


import gcs.Negocio.Contracto;
import gcs.Negocio.Cliente;
import gcs.Negocio.Servico;
import gcs.Negocio.Actividade;
import java.util.ArrayList;
import java.util.Calendar;
import java.util.TreeMap;
import javax.swing.DefaultListModel;
import javax.swing.JOptionPane;

/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

/*
 * AddAct_Contr.java
 *
 * Created on Jan 10, 2010, 8:55:06 PM
 */

/**
 *
 * @author Samuel
 */
public class AddAct_Contr extends javax.swing.JFrame {

    /** Creates new form AddAct_Contr */
    private static Contracto contr;
    GCSView gcsv;
    GCS gcs;
    DefaultListModel model_actividades = new DefaultListModel();
    DefaultListModel model_actividades_contr = new DefaultListModel();
    public AddAct_Contr(Contracto c, GCSView gv,int f) {
        initComponents();
        contr = new Contracto(c);
        gcsv=gv;
        gcs = new GCS();
        
        jTextField1.setEnabled(false);
        jLabel8.setText("Data: "+c.getDatainicio().get(Calendar.DAY_OF_MONTH)+"-"+c.getDatainicio().get(Calendar.MONTH)+"-"+c.getDatainicio().get(Calendar.YEAR));
        jLabel3.setText("Funcionário: "+GCS.getUtilizadorLoggado().getUser());
        jLabel4.setText("Cliente: "+GCS.getClientes().get(c.getCodCliente()).getNome());
        jLabel5.setText("Empresa: "+GCS.getEmpresas().get(c.getCodEmpresa()).getNome());
        jLabel6.setText("Tipo de Serviço: "+GCS.getTipoServico().get(GCS.getEmpresa(c.getCodEmpresa()).getTipo()));
        Servico s = GCS.getServico(c.getCodEmpresa(), c.getCodServico());
        jLabel7.setText("Serviço:" +s.getNome());
        if(f==0){
            for(Actividade a : s.getActividades().values())
                model_actividades.addElement(a.getcodAct()+": "+a.getNome());
            jTextField1.setText("0");
        }
        else{
            jButton3.setVisible(false);
            jLabel9.setText("Adicionar/Remover Actividades ao Contrato");
            jTextField1.setText(""+c.getTotal());
            for(Actividade a : s.getActividades().values()){
                if(c.getListaActividades().values().contains(a))
                    model_actividades_contr.addElement(a.getcodAct()+": "+a.getNome());
                else
                    model_actividades.addElement(a.getcodAct()+": "+a.getNome());
            }
        }
        jList2.setModel(model_actividades);
        jList1.setModel(model_actividades_contr);
    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jScrollPane1 = new javax.swing.JScrollPane();
        jList1 = new javax.swing.JList();
        jScrollPane2 = new javax.swing.JScrollPane();
        jList2 = new javax.swing.JList();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jButton1 = new javax.swing.JButton();
        jButton2 = new javax.swing.JButton();
        jButton3 = new javax.swing.JButton();
        jButton4 = new javax.swing.JButton();
        jButton5 = new javax.swing.JButton();
        jLabel9 = new javax.swing.JLabel();
        jLabel10 = new javax.swing.JLabel();
        jTextField1 = new javax.swing.JTextField();

        setDefaultCloseOperation(javax.swing.WindowConstants.DO_NOTHING_ON_CLOSE);

        jScrollPane1.setName("jScrollPane1"); // NOI18N

        jList1.setName("jList1"); // NOI18N
        jScrollPane1.setViewportView(jList1);

        jScrollPane2.setName("jScrollPane2"); // NOI18N

        jList2.setName("jList2"); // NOI18N
        jScrollPane2.setViewportView(jList2);

        jLabel1.setText("Actividades no Contrato");
        jLabel1.setName("jLabel1"); // NOI18N

        jLabel2.setText("Actividades");
        jLabel2.setName("jLabel2"); // NOI18N

        jLabel3.setText("jLabel3");
        jLabel3.setName("jLabel3"); // NOI18N

        jLabel4.setText("jLabel4");
        jLabel4.setName("jLabel4"); // NOI18N

        jLabel5.setText("jLabel5");
        jLabel5.setName("jLabel5"); // NOI18N

        jLabel6.setText("jLabel6");
        jLabel6.setName("jLabel6"); // NOI18N

        jLabel7.setText("jLabel7");
        jLabel7.setName("jLabel7"); // NOI18N

        jLabel8.setText("jLabel8");
        jLabel8.setName("jLabel8"); // NOI18N

        jButton1.setText("<");
        jButton1.setName("jButton1"); // NOI18N
        jButton1.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton1MouseClicked(evt);
            }
        });

        jButton2.setText(">");
        jButton2.setName("jButton2"); // NOI18N
        jButton2.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton2MouseClicked(evt);
            }
        });

        jButton3.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gcs/Interface/resources/bt-left-icon.png"))); // NOI18N
        jButton3.setText("Retroceder");
        jButton3.setName("jButton3"); // NOI18N
        jButton3.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton3MouseClicked(evt);
            }
        });

        jButton4.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gcs/Interface/resources/new-meeting.png"))); // NOI18N
        jButton4.setText("Finalizar Contrato");
        jButton4.setName("jButton4"); // NOI18N
        jButton4.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton4ActionPerformed(evt);
            }
        });

        jButton5.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gcs/Interface/resources/Cancel-icon.png"))); // NOI18N
        jButton5.setText("Cancelar");
        jButton5.setName("jButton5"); // NOI18N
        jButton5.addMouseListener(new java.awt.event.MouseAdapter() {
            public void mouseClicked(java.awt.event.MouseEvent evt) {
                jButton5MouseClicked(evt);
            }
        });
        jButton5.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButton5ActionPerformed(evt);
            }
        });

        jLabel9.setFont(new java.awt.Font("Lucida Grande", 3, 24));
        jLabel9.setText("Criaçao do Contrato");
        jLabel9.setName("jLabel9"); // NOI18N

        jLabel10.setText("Total");
        jLabel10.setName("jLabel10"); // NOI18N

        jTextField1.setName("jTextField1"); // NOI18N

        org.jdesktop.layout.GroupLayout layout = new org.jdesktop.layout.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(layout.createSequentialGroup()
                .addContainerGap()
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(jLabel3)
                            .add(jLabel4)
                            .add(jLabel5)
                            .add(jLabel6)
                            .add(jLabel7)
                            .add(jLabel8))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, 112, Short.MAX_VALUE)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING, false)
                            .add(layout.createSequentialGroup()
                                .add(95, 95, 95)
                                .add(jButton3)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                                .add(jButton4)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                .add(jButton5))
                            .add(layout.createSequentialGroup()
                                .add(14, 14, 14)
                                .add(jLabel1)
                                .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                .add(jLabel2)
                                .add(51, 51, 51))
                            .add(layout.createSequentialGroup()
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.TRAILING)
                                    .add(layout.createSequentialGroup()
                                        .add(jLabel10)
                                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                                        .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 68, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                    .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 178, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                                .add(16, 16, 16)
                                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                                    .add(jButton1)
                                    .add(jButton2))
                                .add(25, 25, 25)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 179, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .add(88, 88, 88))
                    .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                        .add(jLabel9)
                        .add(237, 237, 237))))
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
            .add(org.jdesktop.layout.GroupLayout.TRAILING, layout.createSequentialGroup()
                .addContainerGap(23, Short.MAX_VALUE)
                .add(jLabel9)
                .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                    .add(layout.createSequentialGroup()
                        .add(8, 8, 8)
                        .add(jLabel8)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(jLabel3)
                        .add(14, 14, 14)
                        .add(jLabel4)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel5)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel6)
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(jLabel7))
                    .add(layout.createSequentialGroup()
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.UNRELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jLabel1)
                            .add(jLabel2))
                        .add(1, 1, 1)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.LEADING)
                            .add(layout.createSequentialGroup()
                                .add(53, 53, 53)
                                .add(jButton1)
                                .add(11, 11, 11)
                                .add(jButton2))
                            .add(layout.createSequentialGroup()
                                .add(3, 3, 3)
                                .add(jScrollPane2, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE))
                            .add(layout.createSequentialGroup()
                                .add(1, 1, 1)
                                .add(jScrollPane1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, 220, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)))
                        .addPreferredGap(org.jdesktop.layout.LayoutStyle.RELATED)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jTextField1, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE, org.jdesktop.layout.GroupLayout.DEFAULT_SIZE, org.jdesktop.layout.GroupLayout.PREFERRED_SIZE)
                            .add(jLabel10))
                        .add(38, 38, 38)
                        .add(layout.createParallelGroup(org.jdesktop.layout.GroupLayout.BASELINE)
                            .add(jButton5)
                            .add(jButton4)
                            .add(jButton3))))
                .addContainerGap())
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

    private void jButton1MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton1MouseClicked
        // TODO add your handling code here:
        if(jList2.getSelectedValue()!=null){
            String s = parse_cod(jList2.getSelectedValue().toString());
            model_actividades_contr.addElement(jList2.getSelectedValue());
            model_actividades.remove(jList2.getSelectedIndex());
            jList1.setModel(model_actividades_contr);
            jList2.setModel(model_actividades);
            float tot = Float.parseFloat(jTextField1.getText());
            Actividade a = GCS.getActividade(contr.getCodEmpresa(), contr.getCodServico(), s);
            tot+=a.getPreco();
            jTextField1.setText(""+tot);

        }

    }//GEN-LAST:event_jButton1MouseClicked

    private void jButton2MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton2MouseClicked
        // TODO add your handling code here:
        if(jList1.getSelectedValue()!=null){
            String s = parse_cod(jList1.getSelectedValue().toString());
            model_actividades.addElement(jList1.getSelectedValue());
            model_actividades_contr.remove(jList1.getSelectedIndex());
            jList1.setModel(model_actividades_contr);
            jList2.setModel(model_actividades);
            float tot = Float.parseFloat(jTextField1.getText());
            Actividade a = GCS.getActividade(contr.getCodEmpresa(), contr.getCodServico(), s);
            tot-=a.getPreco();
            jTextField1.setText(""+tot);
        }
    }//GEN-LAST:event_jButton2MouseClicked

    private void jButton3MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton3MouseClicked
        // TODO add your handling code here:
        this.dispose();
        criaContracto cc = new criaContracto(contr.getCodCliente(),gcsv);
        cc.setVisible(true);
    }//GEN-LAST:event_jButton3MouseClicked

    private void jButton5MouseClicked(java.awt.event.MouseEvent evt) {//GEN-FIRST:event_jButton5MouseClicked
        // TODO add your handling code here:
    }//GEN-LAST:event_jButton5MouseClicked

    private void jButton5ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton5ActionPerformed
        // TODO add your handling code here:
        int n = JOptionPane.showConfirmDialog(rootPane,"Quer mesmo cancelar o criação do contrato","Cancelar Contrato", JOptionPane.YES_NO_OPTION);
        if (n!=1){
            this.dispose();
            gcsv.EnableTrue();
            gcsv.detalhes_tab2(false);
        }

    }//GEN-LAST:event_jButton5ActionPerformed

    private void jButton4ActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButton4ActionPerformed
        // TODO add your handling code here:
        ArrayList<Actividade> la;
        if(model_actividades_contr.size()==0)
                JOptionPane.showMessageDialog(rootPane, "O contrato tem que ter pelo menos 1 actividade");
        else{
            int n = JOptionPane.showConfirmDialog(rootPane,"Deseja criar o contrato","Criar Contrato", JOptionPane.YES_NO_OPTION);
            if(n!=1){
                la=new ArrayList<Actividade>();
                for(int i=0;i<model_actividades_contr.getSize();i++){

                    String s = parse_cod(model_actividades_contr.get(i).toString());
                    la.add(GCS.getActividade(contr.getCodEmpresa(), contr.getCodServico(), s));
                }
                
                contr.alterarActividades(la);

                //inserir entrada no historico
                Cliente cli = GCS.getClientes().get(contr.getCodCliente());
                if(cli.getContractos().get(contr.getCodContracto())==null){
                    gcs.inseriHist(cli, "criou um novo contracto ("+contr.getCodContracto()+").");
                }else gcs.inseriHist(cli, "adicionou/removeu actividades ao contracto ("+contr.getCodContracto()+").");
                
                gcs.criaContr(contr);
                this.dispose();
                gcsv.EnableTrue();
                gcsv.detalhes_tab2(false);
            }
            

        }
        gcsv.actualizaConts();
    }//GEN-LAST:event_jButton4ActionPerformed


        private String parse_cod(String cod){
        String clean = "";
        String[] strs;
        strs = cod.split(":");
        clean = strs[0];
        return clean;
    }

    /**
    * @param args the command line arguments
    */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {
            public void run() {
//                new AddAct_Contr(contr).setVisible(true);
            }
        });
    }

    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButton1;
    private javax.swing.JButton jButton2;
    private javax.swing.JButton jButton3;
    private javax.swing.JButton jButton4;
    private javax.swing.JButton jButton5;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JLabel jLabel9;
    private javax.swing.JList jList1;
    private javax.swing.JList jList2;
    private javax.swing.JScrollPane jScrollPane1;
    private javax.swing.JScrollPane jScrollPane2;
    private javax.swing.JTextField jTextField1;
    // End of variables declaration//GEN-END:variables

}
